﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using HugMe.Bussiness.Model.vk;
using HugMe.DataLayer;

namespace HugMe.Bussiness.Model
{
	public class VkPerson
	{
		public static object Get(string personId, string partnerId)
		{
			return SqlRepository.ExecuteProcedureReader<object>("vk_AddPartner",
			                                             new List<SpParam>
				                                             {
					                                             new SpParam {Name = "@personId", Value = personId, Type = DbType.Guid},
					                                             new SpParam
						                                             {
							                                             Name = "@partnerId",
							                                             Value = partnerId,
							                                             Type = DbType.Guid
						                                             },
				                                             }, null,
			                                             (reader) =>
				                                             {
					                                             bool result = false;
					                                             while (reader.Read())
					                                             {
						                                             result = reader.GetBoolean("Bingo");
					                                             }

					                                             return (object) result;

				                                             });

		}

		public static List<Person> CompleteList(string personId)
		{
			return SqlRepository.ExecuteProcedureReader("vk_CompleteList",
			                                             new List<SpParam>
				                                             {
					                                             new SpParam {Name = "@userId", Value = personId, Type = DbType.Guid},
				                                             }, null,
			                                             (reader) =>
				                                             {
					                                             List<Person> list = new List<Person>();
					                                             while (reader.Read())
					                                             {
						                                             list.Add(new Person()
							                                                      {
																																			UserId = reader.GetString("PartnerId"),
																																			CreateDate = reader.GetDateTimeOrNull("CreateDate"),
							                                                      });
					                                             }

																											 return list;

				                                             });

		}

		public static object GetStatuses(string userId, Person[] model)
		{
			StringBuilder sb = new StringBuilder();
			foreach (var person in model.ToList())
			{
				sb.Append(person.UserId + "###");
			}

			string partners = sb.ToString().TrimEnd('#');

			return SqlRepository.ExecuteProcedureReader<object>("vk_GetPartners",
																									 new List<SpParam>
				                                             {
					                                             new SpParam {Name = "@userId", Value = userId, Type = DbType.Guid},
					                                             new SpParam {Name = "@partners", Value = partners, Type = DbType.Guid},
				                                             }, null,
			                                                    (reader) =>
				                                                    {
																															int count = 0;
																															int batchNum = 0;
																															do
																															{
																																switch (batchNum)
																																{
																																	case 0:
																																		while (reader.Read())
																																		{
																																			string partnerId = reader.GetString("PartnerId");
																																			bool isComplete = reader.GetBoolean("Complete");

																																			var list = model.Where(x => x.UserId == partnerId).ToList();

																																			foreach (var person in list)
																																			{
																																				person.RelationStatus = isComplete
																																					                        ? Person.StatusType.Complete
																																					                        : Person.StatusType.InProgress;
																																			}
																																		}
																																		break;
																																	case 1:
																																		while (reader.Read())
																																		{
																																			count = reader.GetInt32("Count");
																																		}
																																		break;
																																	default:
																																		throw new ApplicationException("Additional batch select");

																																}
																																batchNum++;
																															} while (reader.NextResult());

																															return (object)count;
																														});

		}

		public static void Approve(string getUserId, string partnerId)
		{
			SqlRepository.ExecuteProcedureReader("vk_Approve",
																									 new List<SpParam>
				                                             {
					                                             new SpParam {Name = "@personId", Value = getUserId, Type = DbType.Guid},
					                                             new SpParam
						                                             {
							                                             Name = "@partnerId",
							                                             Value = partnerId,
							                                             Type = DbType.Guid
						                                             },
				                                             }, null,
																									 (reader) =>
																										 {
																											 return new object();

																										 });
		}
	}
}